package com.coder4j.log.controller;

import com.coder4j.log.entity.BizLogEntity;
import com.coder4j.log.service.IBizLogService;
import com.coder4j.log.vo.BizLogQuery;
import com.coder4j.log.vo.BizLogVO;
import com.coder4j.log.vo.BizLogEO;
import com.coder4j.starter.web.api.ApiNote;
import com.coder4j.starter.mybatis.BasePage;
import com.coder4j.starter.tools.vo.R;
import com.coder4j.starter.web.util.EasyExcelUtils;
import com.coder4j.starter.tools.util.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 业务日志
 * @author keller
 * @date 2024-05-07 18:18:56
 */
@Slf4j
@RestController
@RequestMapping("/bizLog")
public class BizLogController {
    @Resource
    private IBizLogService service;

    @Resource
    private HttpServletResponse response;

    /**
     * 按ID查询业务日志
     * @param id BizLog.id
     * @return 业务日志对象
     */
    @GetMapping("/getById")
    @ApiNote(name="按ID查询业务日志",createName = "keller",createTime = "2024-05-07 18:18:56")
    public R<BizLogVO> getById( Long id){

        log.info("====== BizLogControllerGetById ====== {}",id);
        return R.success(service.baseGetVOById(id));
    }

    /**
     * 查询列表
     * @param query 查询条件
     * @return 数据列表
     */
    @PostMapping("/page")
    @ApiNote(name="查询业务日志列表",createName = "keller",createTime = "2024-05-07 18:18:56")
    public R<BasePage<BizLogVO>> getList(@RequestBody BizLogQuery query){
        if(query == null){
            query = new BizLogQuery();
        }
        log.info("====== BizLogControllerGetList ====== {}",query);
        return R.success(service.baseQueryVOPage(query));
    }

    /**
     * 导出列表
     * @param query 查询条件
     */
    @PostMapping("/export")
    @ApiNote(name="导出业务日志列表",createName = "keller",createTime = "2024-05-07 18:18:56")
    public void export(@RequestBody BizLogQuery query){
        if(query == null){
            query = new BizLogQuery();
        }
        log.info("====== BizLogControllerGetList ====== {}",query);
        List<BizLogEntity> list = service.baseQueryList(query);
                List<BizLogEO> eoList = new ArrayList<>();
                for (BizLogEntity entity : list) {
                    BizLogEO eo = new BizLogEO();
                    BeanUtils.copyProperties(entity,eo);
                    eoList.add(eo);
                }
                EasyExcelUtils.exportClass(response,"BizLogList",eoList);
    }


    /**
     * 删除业务日志
     * 如果Entity继承了 BaseSoftDeleteEntity，使用逻辑删除；否则，使用物理删除
     * @param id BizLog.id
     * @return 删除的数据
     */
    @PostMapping("/deleteById")
    @ApiNote(name="删除业务日志",createName = "keller",createTime = "2024-05-07 18:18:56")
    public R deleteById( Long id){
        log.info("====== BizLogControllerDeleteById ======  {}",id);
        service.baseDeleteById(id);
        return R.success();
    }
}
